package org.korecky.gnotedigitalorganizer;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.util.UUID;
import javax.swing.ButtonGroup;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextPane;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.FileSystemView;
import org.korecky.gnotedigitalorganizer.settings.DeviceSettings;
import org.korecky.gnotedigitalorganizer.settings.DeviceSettings.OrientationEnum;

public class ApproveNewDeviceWindow extends JDialog {

    private File device;
    private final JPanel contentPanel = new JPanel();
    private JLabel lblLogo;
    private JTextPane textPane;
    private JLabel blbLandscape;
    private JLabel lblPortrait;
    private JRadioButton rdbtnPortrait;
    private JRadioButton rdbtnLandscape;

    /**
     * Create the dialog.
     */
    public ApproveNewDeviceWindow(File device) {
	this.device = device;
	initComponents();
	FileSystemView fsv = FileSystemView.getFileSystemView();
	textPane.setText("<b>Wold you like add this device to the application?</b><br/>" + "&nbsp;&nbsp;&nbsp;Drive: " + device + "<br/>" + "&nbsp;&nbsp;&nbsp;Display name: " + fsv.getSystemDisplayName(device) + "<br/><br/><br/><u>Device orientation:</u>");
    }

    private void initComponents() {
	ButtonGroup buttonGroup = new ButtonGroup();
	setIconImage(Toolkit.getDefaultToolkit().getImage(ApproveNewDeviceWindow.class.getResource("/org/korecky/gnotedigitalorganizer/resources/icons128x128/usb.png")));
	setTitle("Pair with new device");
	setBounds(100, 100, 520, 366);
	getContentPane().setLayout(new BorderLayout());
	contentPanel.setBackground(Color.WHITE);
	this.contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
	getContentPane().add(this.contentPanel, BorderLayout.CENTER);

	this.lblLogo = new JLabel("");
	this.lblLogo.setIcon(new ImageIcon(ApproveNewDeviceWindow.class.getResource("/org/korecky/gnotedigitalorganizer/resources/icons128x128/usb_complete.png")));

	this.textPane = new JTextPane();
	this.textPane.setContentType("text/html");

	this.blbLandscape = new JLabel("");
	this.blbLandscape.addMouseListener(new MouseAdapter() {
	    @Override
	    public void mouseClicked(MouseEvent e) {
		blbLandscapeMouseClicked(e);
	    }
	});
	this.blbLandscape.setIcon(new ImageIcon(ApproveNewDeviceWindow.class.getResource("/org/korecky/gnotedigitalorganizer/resources/icons64x64/paper-landsacape.png")));

	this.lblPortrait = new JLabel("");
	this.lblPortrait.addMouseListener(new MouseAdapter() {
	    @Override
	    public void mouseClicked(MouseEvent e) {
		lblPortraitMouseClicked(e);
	    }
	});
	this.lblPortrait.setIcon(new ImageIcon(ApproveNewDeviceWindow.class.getResource("/org/korecky/gnotedigitalorganizer/resources/icons64x64/paper-portrait.png")));

	this.rdbtnPortrait = new JRadioButton("Portrait orientation (e.g G-Note 7100)");
	this.rdbtnPortrait.setSelected(true);
	this.rdbtnPortrait.setBackground(Color.WHITE);
	buttonGroup.add(this.rdbtnPortrait);

	this.rdbtnLandscape = new JRadioButton("Landscape orientation (e.g G-Note 5000)");
	this.rdbtnLandscape.setBackground(Color.WHITE);
	buttonGroup.add(this.rdbtnLandscape);
	GroupLayout gl_contentPanel = new GroupLayout(contentPanel);
	gl_contentPanel.setHorizontalGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING).addGroup(gl_contentPanel.createSequentialGroup().addContainerGap().addComponent(this.lblLogo).addGap(18).addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING).addGroup(gl_contentPanel.createSequentialGroup().addComponent(this.lblPortrait).addGap(6).addComponent(this.rdbtnPortrait, GroupLayout.DEFAULT_SIZE, 262, Short.MAX_VALUE)).addGroup(gl_contentPanel.createSequentialGroup().addComponent(this.blbLandscape).addGap(6).addComponent(this.rdbtnLandscape, GroupLayout.DEFAULT_SIZE, 262, Short.MAX_VALUE)).addComponent(this.textPane, GroupLayout.DEFAULT_SIZE, 328, Short.MAX_VALUE)).addContainerGap()));
	gl_contentPanel.setVerticalGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING).addGroup(gl_contentPanel.createSequentialGroup().addContainerGap().addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING).addComponent(this.lblLogo, GroupLayout.DEFAULT_SIZE, 274, Short.MAX_VALUE).addGroup(gl_contentPanel.createSequentialGroup().addComponent(this.textPane, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING).addGroup(gl_contentPanel.createSequentialGroup().addGap(6).addComponent(this.lblPortrait)).addGroup(gl_contentPanel.createSequentialGroup().addGap(27).addComponent(this.rdbtnPortrait))).addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING).addGroup(gl_contentPanel.createSequentialGroup().addPreferredGap(ComponentPlacement.RELATED).addComponent(this.blbLandscape)).addGroup(gl_contentPanel.createSequentialGroup().addGap(27).addComponent(this.rdbtnLandscape))).addPreferredGap(ComponentPlacement.RELATED, 14, Short.MAX_VALUE))).addContainerGap()));
	contentPanel.setLayout(gl_contentPanel);
	{
	    JPanel buttonPane = new JPanel();
	    buttonPane.setBackground(Color.WHITE);
	    buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
	    getContentPane().add(buttonPane, BorderLayout.SOUTH);
	    {
		JButton btnYes = new JButton("YES");
		btnYes.addActionListener(new ActionListener() {
		    public void actionPerformed(ActionEvent arg0) {
			btnYesActionPerformed(arg0);
		    }
		});
		btnYes.setIcon(new ImageIcon(ApproveNewDeviceWindow.class.getResource("/org/korecky/gnotedigitalorganizer/resources/icons16x16/dialog-apply.png")));
		btnYes.setActionCommand("OK");
		buttonPane.add(btnYes);
		getRootPane().setDefaultButton(btnYes);
	    }
	    {
		JButton btnCancel = new JButton("Cancel");
		btnCancel.setIcon(new ImageIcon(ApproveNewDeviceWindow.class.getResource("/org/korecky/gnotedigitalorganizer/resources/icons16x16/dialog-cancel.png")));
		btnCancel.setActionCommand("Cancel");
		buttonPane.add(btnCancel);
	    }
	}
    }

    protected void lblPortraitMouseClicked(MouseEvent e) {
	this.rdbtnPortrait.setSelected(true);
    }

    protected void blbLandscapeMouseClicked(MouseEvent e) {
	this.rdbtnLandscape.setSelected(true);
    }

    protected void btnYesActionPerformed(ActionEvent arg0) {
	try {
	    DeviceSettings deviceSettings = new DeviceSettings();
	    UUID deviceID = UUID.randomUUID();
	    deviceSettings.setId(deviceID.toString());
	    if (rdbtnLandscape.isSelected())
		deviceSettings.setOrientation(OrientationEnum.LANDSCAPE);
	    else if (rdbtnPortrait.isSelected())
		deviceSettings.setOrientation(OrientationEnum.PORTRAIT);
	    deviceID = DeviceManager.pairDeviceWihApplication(device.getAbsoluteFile(), deviceSettings);
	    JOptionPane.showMessageDialog(null, "Device was sucessfully connected with G-Note Digital Ortganizer.", "Congratulations", JOptionPane.INFORMATION_MESSAGE);
	    dispose();
	}
	catch (Exception e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}

    }
}
